package com.yuns.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import javax.swing.text.html.FormSubmitEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by luwenchao on 2020-06-06.
 */
public class SmsUtils {
    /**
     * 发送短信
     *
     * @param mobile
     * @param sms
     * @return
     * @throws ClientException
     */
    public static boolean sendSms(String mobile, String sms,String conText)  {
        Map<String, Object> result = new HashMap<String, Object>();
        String signName = "硒泉供水";

        String phoneNumbers = mobile;
        String accessKeyId = "LTAIcXoFo20b63GP";
        String accessSecret = "hpljlgzkCY9CmLzNZAlSoRcpiIEhi7";

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        DefaultAcsClient defaultAcsClient = new DefaultAcsClient((IClientProfile) profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", sms);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + "您的水费已经欠费" + "\"}");

        try {
            CommonResponse response = defaultAcsClient.getCommonResponse(request);
            System.out.println(response.getData());
            String Message = JSONObject.parseObject(response.getData()).get("Message").toString();
            if ("OK".equals(Message)) {
                return true;
            } else {
                return false;
            }
        } catch (ServerException e) {
            e.printStackTrace();
            return false;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}
